package test02collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author xhw
 */
public class _HashMaptest {

    public static void main(String[] args) {
        //练习4：创建一个HashMap集合，键是学号(String)，值是学生对象(Student)。存储三个键值对元素，并遍历
        Map<String, Student> studentMap = new HashMap<>();

        Student s1 = new Student("小米", 13);
        Student s2 = new Student("华为", 50);
        Student s3 = new Student("苹果", 14);
        //put是map接口的方法并不是hashmap的方法，不可以直接调用
        //studentMap.put(s1.getName(), s1);

        studentMap.put("001", s1);
        studentMap.put("002", s2);
        studentMap.put("003", s3);

        //根据键值对找值
        Set<String> keySet = studentMap.keySet();
        for (String key :
                keySet) {
            Student value = studentMap.get(key);
            System.out.println(key + "=> " + value.getName() + value.getAge());
        }

        //根据对象找到值
        Set<Map.Entry<String, Student>> entrySet = studentMap.entrySet();
        for (Map.Entry<String, Student> me :
                entrySet) {
            String key = me.getKey();
            Student value = me.getValue();
            System.out.println(key + "," + value.getName() + "," + value.getAge());
        }

    }

}
